import {query as queryUsers, queryCurrent, queryUserDetail, changePassword, pwdrule, userfilespace} from '@/services/user';
import {saveOrg, saveRoles, saveUser,saveConfigs} from '@/utils/bwdautil';
import {setAuthority} from '@/utils/authority';

export default {
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
    percentage: 0,
  },

  effects: {
    * fetch(_, {call, put}) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    * fetchCurrent(_, {call, put}) {
      const response = yield call(queryUserDetail);
      if (response.status == 'fail') {
        return;
      }
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },

    *changePassword({ payload, callback }, { call, put }) {
      const response = yield call(changePassword, payload);
      if (response.status == 'fail') {
        return;
      }
      yield put({
        type: 'savePass',
        payload: response,
      });
      if (callback) callback(response);
    },

    *pwdruleF({ payload, callback }, { call, put }) {
      const response = yield call(pwdrule, payload);
      if (response.status == 'fail') {
        return;
      }
      yield put({
        type: 'savePwdrule',
        payload: response,
      });
      if (callback) callback(response);
    },
    *queryUFS({ payload, callback }, { call, put }) {
      const response = yield call(userfilespace, payload);
      if (response.status == 'fail') {
        return;
      }
      yield put({
        type: 'saveUFS',
        payload: response,
      });
      if (callback) callback(response);
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveUFS(state, action) {
      if (action.payload.result == 'ok') {
        const percentage = ((action.payload.data.usedSize/action.payload.data.totalSize)*100).toFixed(2)
        return {
        ...state,
        percentage,
      };
      }

    },
    saveCurrentUser(state, action) {
      let data = action.payload.data;
      let userInfo = {};
      if (data) {
        saveUser(data.userinfo);
        saveOrg(data.org);
        saveRoles(data.roles);
        saveConfigs(data.configs);

        let roles = data.roles||[];
        let tempRole = [];
        roles.forEach(item => {
          tempRole.push(item.roleCode);
        });
        setAuthority(tempRole);


        //先构造假的
        userInfo = {
          name: data.userinfo.userName,
          userid: data.userinfo.userId,
          email: data.userinfo.email,
          notifyCount: 12,
          unreadCount: 11,
          country: 'China',
          address: '西湖区工专路 77 号',
          phone: data.userinfo.mobilePhone,
          pwdEdit: data.userinfo.pwdEdit,
        };
      }
      let roleName = data.roles&&data.roles.length?data.roles.map((v) => {return v.roleName}).join('、'):'';
      if(action.payload.rescode == 200){
        return {
        ...state,
        currentUser: Object.assign(data.userinfo, {roleName:roleName}),
      };
      }

    },
    savePass(state, action) {
      if(action.payload.rescode == 200){
        return {
          ...state,
          currentUser: {},
        };
      }
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
